<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>颜色选择器 colorpicker - Layui 文档</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <script>;!function(){self!==parent&&(location.href="about:blank")}();</script>
  <link href="/public/layui/v/2.8.6/css/layui.css" rel="stylesheet">
  <link href="/public/static/dist/docs/2.8/css/global.css?v=16" rel="stylesheet">
  <script>
  (function(){if(window.JSON&&window.JSON.parse){var c=document.getElementsByTagName("head"),a=document.createElement("link");try{var b=JSON.parse(localStorage.layui)}catch(d){b={}}"dark"===b.themeMode&&(a.href="//res.sentcss.com/static/dist/docs/2.8/css/layui-theme-dark.css?v=16",a.rel="stylesheet",a.id="layuicss-theme-dark",c[0].appendChild(a))}})();
  </script>
</head>
<body>
<div class="layui-header ws-header ws-bg-light">
  <div class="layui-container">
    <div class="ws-logo">
      <a class="logo" href="../../../index.html">
        <img src="/public/static/images/layui/logo.png" alt="layui">
      </a>
    </div>
    <div class="ws-header-assist">
      <div class="layui-form layui-hide-xs ws-search" lay-filter="LAY-ws-header-component"></div>
      <div class="layui-hide-xs ws-notice"></div>
    </div>
    
    <div class="ws-header-menu">
      <ul class="layui-nav layui-bg-gray ws-bg-light" id="LAY_NAV_TOP">
        <li class="layui-nav-item" data-dir="docs">
          <a href="../index.html">组件</a> 
        </li>
        <li class="layui-nav-item" data-dir="notes">
          <a href="../../../notes/index.html">笔记</a> 
        </li>
        <li class="layui-nav-item layui-hide-xs">
          <a href="https://gitee.com/layui/layui/issues" target="_blank" rel="nofollow">社区</a>
        </li>
        <li class="layui-nav-item">
          <a href="javascript:;">
            <!--<span class="layui-badge-dot" style="left:0; right: auto; margin: -4px 0 0 5px;"></span>--> 
            生态
          </a>
          <dl class="layui-nav-child layui-nav-child-r" style="margin-right: -21px;">  
            <dd class="layui-hide-sm layui-show-xs" lay-unselect>
              <a href="https://gitee.com/layui/layui/issues" target="_blank" rel="nofollow">社区</a>
              <hr>
            </dd>
            <dd lay-unselect>
              <a href="https://dev.layuion.com/extend/" target="_blank">Layui 第三方扩展组件</a>
            </dd>
            <dd lay-unselect>
              <a href="https://github.com/Sight-wcg/layui-theme-dark" target="_blank" lay-unselect>
                Layui 深色主题 <span>(社区贡献)</span>
              </a>
            </dd>
            <dd lay-unselect>
              <a href="https://gitee.com/layui/layui-vue/" target="_blank" lay-unselect>
                Layui of Vue <span>(他人实现)</span>
              </a>
            </dd>
          </dl>
        </li>
        <li class="layui-nav-item">
          <a href="javascript:;">2.8.6</a>
          <dl class="layui-nav-child layui-nav-child-c">  
            <dd lay-unselect class="layui-this"><a href="../../../index.html">2.8.6</a></dd>
            <dd lay-unselect><a href="../../../2.7/index.html">2.7.6</a></dd>
          </dl>
        </li>
      </ul>
      <div class="ws-header-more">
        <i class="layui-icon layui-icon-shrink-right"></i>
      </div>
      <div class="ws-header-tool ws-bg-light">
        <div class="layui-form ws-header-theme" lay-filter="header-theme">
          <input type="checkbox" name="theme-mode" id="ID-header-theme-mode" lay-filter="header-theme-mode" lay-skin="switch">
          <div lay-checkbox>
            <i class="layui-icon layui-icon-moon"></i> | 
            <i class="layui-icon layui-icon-light"></i>
          </div>
        </div>
        <div>
          <a href="https://github.com/layui/layui" target="_blank" title="Github">
            <i class="layui-icon layui-icon-github layui-font-20"></i>
          </a>
        </div>
        <div>
          <a href="https://gitee.com/layui/layui" target="_blank" title="Gitee">
            <i class="layui-icon layui-icon-gitee layui-font-20" style="color: #C71D23;"></i>
            <!--<img src="https://gitee.com/static/images/logo-en.svg" width="21">-->
          </a>
        </div>
      </div>
    </div>
  </div>
</div>
<!-- 让 IE8/9 兼容响应式栅格 -->
<!--[if lt IE 9]>
  <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
  <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->
 
<div class="layui-container ws-container">
  
<div class="layui-panel ws-menu">
  <ul class="layui-menu layui-menu-lg ws-docs-menu">
    <li class="layui-menu-item-group" lay-options="{type: 'group', isAllowSpread: true}">
      <div class="layui-menu-body-title">基础</div>
      <ul>
        <li>
          <div class="layui-menu-body-title">
            <a href="../index.html">
              <span>开始使用</span> 
              <span class="layui-font-12 layui-font-gray">Getting Started</span>
            </a>
          </div>
        </li>
        <li>
          <div class="layui-menu-body-title">
            <a href="../base.html">
              <span>底层方法</span>
              <span class="layui-font-12 layui-font-gray">Base Function</span>
            </a>
          </div>
        </li>
        <li>
          <div class="layui-menu-body-title">
            <a href="../modules.html">
              <span>模块系统</span>
              <span class="layui-font-12 layui-font-gray">Modules</span>
              <span class="layui-badge-dot layui-bg-green"></span>
            </a>
          </div>
        </li>
        <li>
          <div class="layui-menu-body-title">
            <a href="../versions.html">
              <span>更新日志</span>
              <span class="layui-font-12 layui-font-gray">Versions</span>
              <span class="layui-badge-dot"></span>
            </a>
          </div>
        </li>
      </ul>
    </li>
    <li class="layui-menu-item-group" lay-options="{type: 'group', isAllowSpread: true}">
      <div class="layui-menu-body-title">布局</div>
      <ul>
        <li>
          <div class="layui-menu-body-title">
            <a href="../layout/index.html">
              <span>框体</span>
              <span class="layui-font-12 layui-font-gray">Layout</span>
            </a>
          </div>
        </li>
        <li>
          <div class="layui-menu-body-title">
            <a href="../layout/grid.html">
              <span>栅格</span>
              <span class="layui-font-12 layui-font-gray">Grid</span>
            </a>
          </div>
        </li>
      </ul>
    </li>
    <li class="layui-menu-item-group" lay-options="{type: 'group', isAllowSpread: true}">
      <div class="layui-menu-body-title">通用</div>
      <ul>
        <li>
          <div class="layui-menu-body-title">
            <a href="../color/index.html">
              <span>颜色</span>
              <span class="layui-font-12 layui-font-gray">Color</span>
              <span class="layui-badge-dot layui-bg-green"></span>
            </a>
          </div>
        </li>
        <li>
          <div class="layui-menu-body-title">
            <a href="../button/index.html">
              <span>按钮</span>
              <span class="layui-font-12 layui-font-gray">Button</span>
              <span class="layui-badge-dot layui-bg-green"></span>
            </a>
          </div>
        </li>
        <li>
          <div class="layui-menu-body-title">
            <a href="../icon/index.html">
              <span>图标</span>
              <span class="layui-font-12 layui-font-gray">Icon</span>
            </a>
          </div>
        </li>
        <li>
          <div class="layui-menu-body-title">
            <a href="../anim/index.html">
              <span>动画</span>
              <span class="layui-font-12 layui-font-gray">Anim</span>
            </a>
          </div>
        </li>
      </ul>
    </li>
    <li class="layui-menu-item-group" lay-options="{type: 'group', isAllowSpread: true}">
      <div class="layui-menu-body-title">表单</div>
      <ul>
        <li>
          <div class="layui-menu-body-title">
            <a href="../form/index.html">
              <span>表单组件</span>
              <span class="layui-font-12 layui-font-gray">Form</span>
              <span class="layui-badge-dot layui-bg-green"></span>
            </a>
          </div>
        </li>
        <li>
          <div class="layui-menu-body-title">
            <a href="../form/input.html">
              <span>输入框</span>
              <span class="layui-font-12 layui-font-gray">Input / Textarea</span>
            </a>
          </div>
        </li>
        <li>
          <div class="layui-menu-body-title">
            <a href="../form/select.html">
              <span>选择框</span>
              <span class="layui-font-12 layui-font-gray">Select</span>
            </a>
          </div>
        </li>
        <li>
          <div class="layui-menu-body-title">
            <a href="../form/checkbox.html">
              <span>复选框</span>
              <span class="layui-font-12 layui-font-gray">Checkbox</span>
            </a>
          </div>
        </li>
        <li>
          <div class="layui-menu-body-title">
            <a href="../form/radio.html">
              <span>单选框</span>
              <span class="layui-font-12 layui-font-gray">Radio</span>
            </a>
          </div>
        </li>
      </ul>
    </li>
    <li class="layui-menu-item-group" lay-options="{type: 'group', isAllowSpread: true}">
      <div class="layui-menu-body-title">展示</div>
      <ul>
        <li>
          <div class="layui-menu-body-title">
            <a href="../table/index.html">
              <span>表格</span>
              <span class="layui-font-12 layui-font-gray">Table</span>
              <span class="layui-badge-dot layui-bg-green"></span>
            </a>
          </div>
        </li>
        <li>
          <div class="layui-menu-body-title">
            <a href="../laypage/index.html">
              <span>分页</span>
              <span class="layui-font-12 layui-font-gray">Laypage</span>
            </a>
          </div>
        </li>
        <li>
          <div class="layui-menu-body-title">
            <a href="../treeTable/index.html">
              <span>树形表格</span>
              <span class="layui-font-12 layui-font-gray">TreeTable</span>
            </a>
          </div>
        </li>
        <li>
          <div class="layui-menu-body-title">
            <a href="../nav/index.html">
              <span>导航菜单</span>
              <span class="layui-font-12 layui-font-gray">Nav</span>
            </a>
          </div>
        </li>
        <li>
          <div class="layui-menu-body-title">
            <a href="../menu/index.html">
              <span>基础菜单</span>
              <span class="layui-font-12 layui-font-gray">Menu</span>
            </a>
          </div>
        </li>
        <li>
          <div class="layui-menu-body-title">
            <a href="../tab/index.html">
              <span>选项卡</span>
              <span class="layui-font-12 layui-font-gray">Tab</span>
            </a>
          </div>
        </li>
        <li>
          <div class="layui-menu-body-title">
            <a href="../badge/index.html">
              <span>徽章</span>
              <span class="layui-font-12 layui-font-gray">Badge</span>
            </a>
          </div>
        </li>
        <li>
          <div class="layui-menu-body-title">
            <a href="../auxiliar/index.html">
              <span>辅助</span>
              <span class="layui-font-12 layui-font-gray">Blockquote / Fieldset / hr</span>
            </a>
          </div>
        </li>
        <li>
          <div class="layui-menu-body-title">
            <a href="../class/index.html">
              <span>公共类</span>
              <span class="layui-font-12 layui-font-gray">Class</span>
            </a>
          </div>
        </li>
        <li>
          <div class="layui-menu-body-title">
            <a href="../panel/index.html">
              <span>面板</span>
              <span class="layui-font-12 layui-font-gray">Panel / Card / Collapse</span>
            </a>
          </div>
        </li>
        <li>
          <div class="layui-menu-body-title">
            <a href="../progress/index.html">
              <span>进度条</span>
              <span class="layui-font-12 layui-font-gray">Progress</span>
            </a>
          </div>
        </li>
        <li>
          <div class="layui-menu-body-title">
            <a href="../timeline/index.html">
              <span>时间线</span>
              <span class="layui-font-12 layui-font-gray">Timeline</span>
            </a>
          </div>
        </li>
        <li>
          <div class="layui-menu-body-title">
            <a href="../fixbar/index.html">
              <span>固定条</span>
              <span class="layui-font-12 layui-font-gray">Fixbar</span>
            </a>
          </div>
        </li>
        <li>
          <div class="layui-menu-body-title">
            <a href="../tree/index.html">
              <span>树组件</span>
              <span class="layui-font-12 layui-font-gray">Tree</span>
            </a>
          </div>
        </li>
        <li>
          <div class="layui-menu-body-title">
            <a href="../carousel/index.html">
              <span>轮播</span>
              <span class="layui-font-12 layui-font-gray">Carousel</span>
            </a>
          </div>
        </li>
        <li>
          <div class="layui-menu-body-title">
            <a href="../flow/index.html">
              <span>流加载</span>
              <span class="layui-font-12 layui-font-gray">Flow</span>
            </a>
          </div>
        </li>
        <li>
          <div class="layui-menu-body-title">
            <a href="../code/index.html">
              <span>代码预览组件</span>
              <span class="layui-font-12 layui-font-gray">Code</span>
            </a>
          </div>
        </li>
      </ul>
    </li>
    <li class="layui-menu-item-group" lay-options="{type: 'group', isAllowSpread: true}">
      <div class="layui-menu-body-title">交互</div>
      <ul>
        <li>
          <div class="layui-menu-body-title">
            <a href="../layer/index.html">
              <span>弹出层</span>
              <span class="layui-font-12 layui-font-gray">Layer</span>
              <span class="layui-badge-dot layui-bg-green"></span>
            </a>
          </div>
        </li>
        <li>
          <div class="layui-menu-body-title">
            <a href="../laydate/index.html">
              <span>日期与时间选择</span>
              <span class="layui-font-12 layui-font-gray">Laydate</span>
            </a>
          </div>
        </li>
        <li>
          <div class="layui-menu-body-title">
            <a href="../upload/index.html">
              <span>上传</span>
              <span class="layui-font-12 layui-font-gray">Upload</span>
            </a>
          </div>
        </li>
        <li>
          <div class="layui-menu-body-title">
            <a href="../dropdown/index.html">
              <span>下拉菜单</span>
              <span class="layui-font-12 layui-font-gray">Dropdown</span>
            </a>
          </div>
        </li>
        <li>
          <div class="layui-menu-body-title">
            <a href="index.html">
              <span>颜色选择器</span>
              <span class="layui-font-12 layui-font-gray">Colorpicker</span>
            </a>
          </div>
        </li>
        <li>
          <div class="layui-menu-body-title">
            <a href="../transfer/index.html">
              <span>穿梭框</span>
              <span class="layui-font-12 layui-font-gray">Transfer</span>
            </a>
          </div>
        </li>
        <li>
          <div class="layui-menu-body-title">
            <a href="../slider/index.html">
              <span>滑块</span>
              <span class="layui-font-12 layui-font-gray">Slider</span>
            </a>
          </div>
        </li>
        <li>
          <div class="layui-menu-body-title">
            <a href="../rate/index.html">
              <span>评分</span>
              <span class="layui-font-12 layui-font-gray">Rate</span>
            </a>
          </div>
        </li>
      </ul>
    </li>
    <li class="layui-menu-item-group" lay-options="{type: 'group', isAllowSpread: true}">
      <div class="layui-menu-body-title">其他</div>
      <ul>
        <li>
          <div class="layui-menu-body-title">
            <a href="../laytpl/index.html">
              <span>模板引擎</span>
              <span class="layui-font-12 layui-font-gray">Laytpl</span>
            </a>
          </div>
        </li>
        <li>
          <div class="layui-menu-body-title">
            <a href="../util/index.html">
              <span>工具模块</span>
              <span class="layui-font-12 layui-font-gray">Util</span>
            </a>
          </div>
        </li>
      </ul>
    </li>
    
  </ul>
</div>

  <div class="ws-content">
    <div class="layui-text ws-text" id="WS-text">
      <h1>颜色选择器</h1>
<blockquote class="layui-elem-quote">
<p>颜色选择器 <code>colorpicker</code> 用于对颜色的快捷选择，支持 <code>hex,rgb,rgba</code> 三种颜色类型。</p>
</blockquote class="layui-elem-quote">
<h2 id="examples" lay-toc="{hot: true}" style="margin-bottom: 0;">示例</h2>
<div>
<pre class="layui-code" lay-options="{preview: true, text: {preview: '常规使用'}, layout: ['preview', 'code'], tools: ['full']}">
  <textarea>
<div id="ID-colorpicker-demo-1"></div>
<div id="ID-colorpicker-demo-2" style="margin-left: 16px;"></div>
<!-- import layui --> 
<script>
layui.use(function(){
  var colorpicker = layui.colorpicker;
  var layer = layui.layer;
  // 渲染
  colorpicker.render({ // eg1
    elem: '#ID-colorpicker-demo-1', // 绑定元素
    change: function(color){ // 颜色改变的回调
      layer.tips('选择了：'+ color, this.elem, {
        tips: 1
      });
    }
  });
  colorpicker.render({ // eg2
    elem: '#ID-colorpicker-demo-2',
    color: '#2ec770', // 设置默认色
    done: function(color){ // 选择完毕的回调
      layer.tips('选择了：'+ color, this.elem);
    }
  });
});
</script>
  </textarea>
</pre>
<h3 id="demo-form" class="ws-anchor ws-bold">将颜色值赋给表单</h3>
<pre class="layui-code" lay-options="{preview: true, layout: ['preview', 'code'], tools: ['full']}">
  <textarea>
<form class="layui-form" action="index.html">
  <div class="layui-form-item">
    <div class="layui-input-inline" style="width: 120px;">
      <input type="text" name="color" value="" placeholder="请选择颜色" class="layui-input" id="ID-colorpicker-demo-form-color">
    </div>
    <div class="layui-inline" style="left: -11px;">
      <div id="ID-colorpicker-demo-form"></div>
    </div>
  </div>
</form>
<!-- import layui --> 
<script>
layui.use(function(){
  var colorpicker = layui.colorpicker;
  var $ = layui.$;
  // 渲染
  colorpicker.render({
    elem: '#ID-colorpicker-demo-form',
    color: '#1c97f5',
    done: function(color){
      $('#ID-colorpicker-demo-form-color').val(color);
    }
  });
});
</script>
  </textarea>
</pre>
<h3 id="demo-rgba" class="ws-anchor ws-bold">设置 RGB / RGBA</h3>
<pre class="layui-code" lay-options="{preview: true, layout: ['preview', 'code'], tools: ['full']}">
  <textarea>
<div id="ID-colorpicker-demo-rgb"></div>
<div id="ID-colorpicker-demo-rgba" style="margin-left: 16px;"></div>
<!-- import layui --> 
<script>
layui.use(function(){
  var colorpicker = layui.colorpicker;
  // 渲染
  colorpicker.render({ // eg1
    elem: '#ID-colorpicker-demo-rgb',
    color: 'rgb(68,66,66)',
    format: 'rgb' // 默认为 hex
  }); 
  colorpicker.render({ // eg2
    elem: '#ID-colorpicker-demo-rgba',
    color: 'rgba(68,66,66,0.5)',
    format: 'rgb',
    alpha: true // 开启透明度滑块
  });
});
</script>
  </textarea>
</pre>
<h3 id="demo-alpha" class="ws-anchor ws-bold">开启透明度</h3>
<pre class="layui-code" lay-options="{preview: true, layout: ['preview', 'code'], tools: ['full']}">
  <textarea>
<div id="ID-colorpicker-demo-alpha-1"></div>
<div id="ID-colorpicker-demo-alpha-2" style="margin-left: 16px;"></div>
<div id="ID-colorpicker-demo-alpha-3" style="margin-left: 16px;"></div>
<!-- import layui --> 
<script>
layui.use(function(){
  var colorpicker = layui.colorpicker;
  // 渲染
  colorpicker.render({ // eg1
    elem: '#ID-colorpicker-demo-alpha-1',
    color: '#16baaa', // hex
    alpha: true, // 开启透明度
    format: 'rgb'
  });
  colorpicker.render({ // eg2
    elem: '#ID-colorpicker-demo-alpha-2',
    color: 'rgb(0,150,136,0.6)', // rgba
    alpha: true,
    format: 'rgb'
  });
  colorpicker.render({ // eg3
    elem: '#ID-colorpicker-demo-alpha-3',
    alpha: true,
    format: 'rgb'
  });
});
</script>
  </textarea>
</pre>
<h3 id="demo-predefine" class="ws-anchor ws-bold">预定义颜色项</h3>
<pre class="layui-code" lay-options="{preview: true, layout: ['preview', 'code'], tools: ['full']}">
  <textarea>
<div id="ID-colorpicker-demo-predefine-1"></div>
<div id="ID-colorpicker-demo-predefine-2" style="margin-left: 16px;"></div>
<!-- import layui --> 
<script>
layui.use(function(){
  var colorpicker = layui.colorpicker;
  // 渲染
  colorpicker.render({ // eg1
    elem: '#ID-colorpicker-demo-predefine-1',
    color: '#c71585',
    predefine: true // 开启预定义颜色
  });
  colorpicker.render({ // eg2
    elem: '#ID-colorpicker-demo-predefine-2',
    color: '#9d8a0e',
    predefine: true, // 开启预定义颜色
    colors: ['#ff8c00','#00ced1','#9d8a0e'] //自定义预定义颜色项
  });
});
</script>
  </textarea>
</pre>
<h3 id="demo-all" class="ws-anchor ws-bold">全功能和回调的使用</h3>
<pre class="layui-code" lay-options="{preview: true, layout: ['preview', 'code'], tools: ['full']}">
  <textarea>
<button class="layui-btn" id="ID-colorpicker-demo-all-btn">主题按钮</button>
<hr>
<div id="ID-colorpicker-demo-all"></div> 
<span style="padding-left: 6px;">演示：选择颜色并确定，改变上方按钮背景色</span>
<!-- import layui --> 
<script>
layui.use(function(){
  var colorpicker = layui.colorpicker;
  var layer = layui.layer;
  var $ = layui.$;
  // 渲染
  colorpicker.render({
    elem: '#ID-colorpicker-demo-all',
    color: '#16baaa',
    predefine: true,
    alpha: true,
    done: function(color){
      layer.msg('选择的值：'+ color); // 选择完毕的颜色值
      
      // 清空或取消选择时也执行 change
      color || this.change(color); 
    },
    change: function(color){
      // 给当前页面头部和左侧设置主题色
      $('#ID-colorpicker-demo-all-btn').css('background-color', color);
    },
    cancel: function(color){ // 取消颜色选择的回调 --- 2.8+
      this.change(color);
    }
  });
});
</script>
  </textarea>
</pre>
<h3 id="demo-size" class="ws-anchor ws-bold">颜色框尺寸</h3>
<pre class="layui-code" lay-options="{preview: true, layout: ['preview', 'code'], tools: ['full']}">
  <textarea>
<div id="ID-colorpicker-demo-size-lg"></div>
<div id="ID-colorpicker-demo-size-sm" style="margin-left: 16px;"></div>
<div id="ID-colorpicker-demo-size-xs" style="margin-left: 16px;"></div>
<!-- import layui --> 
<script>
layui.use(function(){
  var colorpicker = layui.colorpicker;
  // 渲染
  colorpicker.render({
    elem: '#ID-colorpicker-demo-size-lg',
    size: 'lg' // 大号下拉框
  });
  colorpicker.render({
    elem: '#ID-colorpicker-demo-size-sm'
    //,size: 'sm' // 默认 sm
  });
  colorpicker.render({
    elem: '#ID-colorpicker-demo-size-xs',
    size: 'xs' // mini 下拉框
  });  
});
</script>
  </textarea>
</pre>
<h3 id="demo-class" class="ws-anchor ws-bold">同时绑定多个元素</h3>
<pre class="layui-code" lay-options="{preview: true, layout: ['preview', 'code'], tools: ['full']}">
  <textarea>
<div class="demo-class-colorpicker" lay-options="{color: '#FF0000'}"></div>
<div class="demo-class-colorpicker" lay-options="{color: '#008000'}"></div>
<div class="demo-class-colorpicker" lay-options="{color: '#0000FF'}"></div>
<!-- import layui --> 
<script>
layui.use(function(){
  var colorpicker = layui.colorpicker;
  // 渲染
  colorpicker.render({
    elem: '.demo-class-colorpicker',
    done: function(color){
      console.log(this.elem, color);
    }
  });
});
</script>
  </textarea>
</pre>
</div>
<p></p>
<h2 id="api" lay-toc="{}">API</h2>
<table class="layui-table">
<thead>
<tr>
<th>API</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr>
<td>var colorpicker = layui.colorpicker</td>
<td>获得 <code>colorpicker</code> 模块。</td>
</tr>
<tr>
<td><a href="index.html#render">colorpicker.render(options)</a></td>
<td>colorpicker 组件渲染，核心方法。</td>
</tr>
</tbody>
</table class="layui-table">
<h2 id="render" lay-toc="{level: 2}">渲染</h2>
<p><code>colorpicker.render(options);</code></p>
<ul>
<li>参数 <code>options</code> : 基础属性配置项。<a href="index.html#options">#详见属性</a>
<br>注 <sup>2.7+</sup> : 除 <code>elem</code> 属性外，其他基础属性也可以直接写在元素的 <code>lay-options=&quot;{}&quot;</code> 属性中。</li>
</ul>
<pre><code class="layui-code">&lt;div id=&quot;ID-test-colorpicker&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;class-test-colorpicker&quot; lay-options=&quot;{color: '#333'}&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;class-test-colorpicker&quot; lay-options=&quot;{color: '#777'}&quot;&gt;&lt;/div&gt;
&lt;!-- import layui --&gt; 
&lt;script&gt;
layui.use(function(){
  var colorpicker = layui.colorpicker;
  // 单个渲染
  colorpicker.render({
    elem: '#ID-test-colorpicker'
  });
  // 批量渲染
  colorpicker.render({
    elem: '.class-test-colorpicker'
  });
});
&lt;/script&gt;
</code></pre>
<h2 id="options" lay-toc="{level: 2, hot: true}">属性</h2>
<div>
<table class="layui-table">
  <colgroup>
    <col width="150">
    <col>
    <col width="100">
    <col width="100">
  </colgroup>
  <thead>
    <tr>
      <th>属性名</th>
      <th>描述</th>
      <th>类型</th>
      <th>默认值</th>
    </tr> 
  </thead>
  <tbody>
    <tr>
<td>elem</td>
<td>
<p>绑定元素选择器或 DOM 对象</p>
</td>
<td>string/DOM</td>
<td>-</td>
    </tr>
    <tr>
<td>color</td>
<td>
<p>默认颜色值，值的格式跟随 <code>format</code> 属性的设定。</p>
</td>
<td>string</td>
<td>-</td>
    </tr>
    <tr>
<td>format</td>
<td>
<p>颜色显示/输入格式，支持 <code>hex</code> <code>rgb</code> 。 若同时开启 <code>alpha</code> 属性，则颜色值自动变为 <code>rgba</code>。</p>
</td>
<td>string</td>
<td>
<p><code>hex</code></p>
</td>
    </tr>
    <tr>
<td>alpha</td>
<td>
<p>是否开启透明度。当同时开启 <code>format: 'rga'</code> 时，<code>color</code> 值将采用 <code>rgba</code> 格式。</p>
</td>
<td>boolean</td>
<td>
<p><code>false</code></p>
</td>
    </tr>
    <tr>
<td>predefine</td>
<td>
<p>是否开启预定义颜色栏</p>
</td>
<td>boolean</td>
<td>
<p><code>false</code></p>
</td>
    </tr>
    <tr>
<td>colors</td>
<td>
<p>设置可选的颜色列表，需开启 <code>predefine: true</code> 有效。
<br>用法详见：<a href="index.html#demo-predefine">#预定义颜色项</a></p>
</td>
<td>array</td>
<td>-</td>
    </tr>
    <tr>
<td>size</td>
<td>
<p>颜色框的尺寸，可选值： <code>lg</code> <code>sm</code> <code>xs</code></p>
</td>
<td>string</td>
<td>
<p><code>sm</code></p>
</td>
    </tr>
    <tr>
<td colspan="4" style="text-align: center"> 
<div id="options.callback" lay-pid="options" class="ws-anchor">
<p><a href="index.html#options.callback">回调函数</a></p>
</div>
</td>
    </tr>
    <tr>
<td>change</td>
<td colspan="3">
<p>颜色被改变的回调函数。用法详见：<a href="index.html#demo-all">#示例</a></p>
<pre><code class="layui-code">change: function(value){
  console.log(value); // 当前颜色值
}
</code></pre>
</td>
    </tr>
    <tr>
<td>done</td>
<td colspan="3">
<p>颜色选择完毕的回调函数。点击“确认”和“清除”按钮均会触发</p>
<pre><code class="layui-code">done: function(value){
  console.log(value); // 当前选中的颜色值
}
</code></pre>
<p>用法详见：<a href="index.html#demo-all">#示例</a></p>
</td>
    </tr>
    <tr>
<td>cancel <sup>2.8+</sup></td>
<td colspan="3">
<p>取消颜色选择的回调函数，一般点击非颜色选择面板区域触发。</p>
<pre><code class="layui-code">cancel: function(value){
  console.log(value); // 当前颜色值
}
</code></pre>
<p>用法详见：<a href="index.html#demo-all">#示例</a></p>
</td>
    </tr>
    <tr>
<td>close <sup>2.8+</sup></td>
<td colspan="3">
<p>颜色选择面板被关闭后即触发。</p>
<pre><code class="layui-code">close: function(value){
  console.log(value); // 当前颜色值
}
</code></pre>
</td>
    </tr>
  </tbody>
</table>
</div>
<h2>兼容性</h2>
<blockquote class="layui-elem-quote">
<p>colorpicker 组件支持 <code>Chrome,Edge,Firefox</code> 等所有高级浏览器，不支持 IE10 低版本浏览器。</p>
</blockquote class="layui-elem-quote">

    </div>
    <div class="ws-text-tools">
      
      <a href="https://github.com/layui/layui/edit/2.x/docs/colorpicker/index.md" rel="nofollow" target="_blank" title="在 Github 上编辑此文档">
        <i class="layui-icon layui-icon-edit"></i>
      </a>
      
    </div>
  </div>
</div>
<div class="ws-menu-bar layui-bg-green layui-hide">
  <i class="layui-icon layui-icon-spread-left"></i>
</div>
  
<div class="layui-footer ws-footer">
  <p>
    Copyright &copy; 2023 <a href="../../../index.html">Layui</a> MIT Licensed</p>
  <p id="LAY-footer-info">
    <a href="https://gitee.com/layui/layui/blob/main/DISCLAIMER.md" rel="nofollow" target="_blank">免责声明</a> 
    <a href="https://github.com/layui/layui/blob/main/LICENSE" rel="nofollow" target="_blank">开源协议</a>
    <a href="https://gitee.com/layui/" target="_blank">开源生态</a>
    <a href="https://codepen.io/layui/pen/bGxZXrd" rel="nofollow" target="_blank">在线测试</a>
    <a href="https://gitee.com/layui/layui/issues" rel="nofollow" target="_blank">社区交流</a>
  </p>
  <p>
    <span>
      鸣谢：
      <a href="https://www.upyun.com/?invite=SJ0wu6g2-&referer=layui" target="_blank" sponsor="upyun" style="color: #23A7E8;">
        <strong>又拍云</strong>
      </a>
      <a href="https://gitee.com/?referer=layui" target="_blank" sponsor="Gitee" style="color:#C71D23;">
        <strong>Gitee</strong>
      </a>
      <a href="https://www.iconfont.cn/?referer=layui" target="_blank" sponsor="Gitee" style="color: #9B16FF;">
        <strong>iconfont</strong>
      </a>
    </span>
  </p>
</div>
<div class="ws-shade"></div>
 
<script src="/public/layui/v/2.8.6/layui.js"></script>
<script>
layui.config({
  base: '//res.sentcss.com/static/dist/docs/2.8/modules/',
  version: '16',
  res: '//res.sentcss.com/static/dist/docs/',
  curl: ''
}).use('global',function(w){w.a||w.r&&w.r()});
</script>
 
<script>
var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?363b2abe8678eec8a6850500ea63a63b";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();
</script>

</body>
</html>